﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace EWDT_CDS_Hub.App_Code
{
    public class AnnToSchool
    {
        public String School_id { get; set; }
        public int Ann_id { get; set; }

        public static List<AnnToSchool> GetAnnToSchool()
        {
            List<AnnToSchool> results = new List<AnnToSchool>();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM AnnToSchool";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    AnnToSchool a = new AnnToSchool(); 
                    a.School_id = (String)dr["school_id"];
                    a.Ann_id = (int)dr["ann_id"];                  

                    results.Add(a);
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return results;
        }

        public static int InsertAnnToSchool(AnnToSchool a)
        {
            int rowsInserted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO AnnToSchool (school_id, ann_id) VALUES (@schid, @annid)";
                comm.Parameters.AddWithValue("@schid", a.School_id);
                comm.Parameters.AddWithValue("@annid", a.Ann_id);
                rowsInserted = comm.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return rowsInserted;
        }

        //retrieve all announcements of a school
        public static List<Announcement> RetrieveAnnOfSchool(String sch_id)
        {
            List<Announcement> results = new List<Announcement>();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM Announcement WHERE ann_id IN (SELECT ann_id FROM AnnToSchool where school_id = @id)";
                comm.Parameters.AddWithValue("@id", sch_id);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    Announcement a = new Announcement();
                    a.Ann_id = (int)dr["ann_id"];
                    a.Ann_title = (String)dr["ann_title"];
                    a.Ann_content = (String)dr["ann_content"];
                    a.Ann_date = (DateTime)dr["ann_date"];
                    a.Ann_priority = (int)dr["ann_priority"];
                    results.Add(a);
                }

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return results;
        }


        public static int DeletAnnToSchool(String school_id, int ann_id)
        {
            int rowsDeleted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "DELETE FROM AnnToSchool WHERE school_id = @schid AND ann_id = @annid";
                comm.Parameters.AddWithValue("@schid", school_id);
                comm.Parameters.AddWithValue("@annid", ann_id);
                rowsDeleted = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return rowsDeleted;
        }
    }
}